package com.tcxhb.job.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcxhb.job.dao.dataobject.JobLockDO;
import com.tcxhb.job.dao.mapper.JobLockMapper;
import com.tcxhb.job.dao.repository.JobLockRepository;
import org.springframework.stereotype.Repository;

/**
 * author:auto.generator
 * time: 2023-11-02
 */
@Repository
public class JobLockRepositoryImpl extends ServiceImpl<JobLockMapper, JobLockDO> implements JobLockRepository {

    @Override
    public Boolean update(String key, String newValue, String oldValue) {
        LambdaUpdateWrapper<JobLockDO> wrapper = Wrappers.<JobLockDO>lambdaUpdate();
        wrapper.eq(JobLockDO::getLockKey, key);
        if (StringUtils.isNotBlank(oldValue)) {
            wrapper.eq(JobLockDO::getValue, oldValue);
        } else {
            wrapper.isNull(JobLockDO::getValue);
        }
        wrapper.set(JobLockDO::getValue, newValue);
        return super.update(wrapper);
    }

    @Override
    public String queryByKey(String key) {
        LambdaQueryWrapper<JobLockDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JobLockDO::getLockKey, key);
        JobLockDO lockDO = super.getOne(wrapper);
        if (lockDO != null) {
            return lockDO.getValue();
        }
        return null;
    }
}
